import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { VueAxios } from './axios'
import { Modal, notification } from 'ant-design-vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
notification.config({
  top: '62px'
})

// const precessEnv = process.env
let baseURL = process.env.VUE_APP_BASE_API
// let baseURL = '/api1'
// console.log(baseURL)
// if (precessEnv.VUE_APP_FLAG === 'dev' || precessEnv.VUE_APP_FLAG === 'local') {
//   // baseURL = ''
// } else {
//   baseURL = process.env.VUE_APP_BASE_API
// }

// 创建 axios 实例
const service = axios.create({
  baseURL: baseURL,
  timeout: 30000 // 请求超时时间
})

const err = error => {
  if (error.response) {
    let data = error.response.data
    switch (error.response.status) {
      // case 403:
      //   notification.error({ message: '系统提示', description: error.response.message, duration: 4 })
      //   break
      case 404:
        notification.error({ message: '系统提示', description: '很抱歉，资源未找到!', duration: 4 })
        break
      case 426:
        notification.error({ message: '系统提示', description: '用户名或密码错误', duration: 4 })
        break
      case 504:
        notification.error({ message: '系统提示', description: '网络超时' })
        break
      case 401: {
        const token = Vue.ls.get(ACCESS_TOKEN)
        if (token) {
          Modal.error({
            title: '登录已过期',
            content: '很抱歉，登录已过期，请重新登录',
            okText: '重新登录',
            mask: false,
            onOk: () => {
              store.dispatch('LogoutOut')
            }
          })
        }
        break
      }
      case 500:
        notification.error({ message: '系统提示', description: '服务器异常' })
        break
      default:
        notification.error({
          message: '系统提示',
          description: data.msg || data.message,
          duration: 4
        })
        break
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(
  config => {
    console.log(config)
    const token = Vue.ls.get(ACCESS_TOKEN)
    if (token) {
      config.headers['token'] = token // token
      config.headers['X-FRAME-OPTIONS'] = 'DENY' // token
    }
    if (config.method === 'get') {
      config.params = {
        _t: Date.parse(new Date()) / 1000,
        ...config.params
      }
    }
    if (process.env.NODE_ENV === 'production') {
      if (config.customData && config.customData.baseUrl) {
        config.url = config.customData.baseUrl + config.url
      }
    }

    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(response => {
  return response.data
}, err)

const installer = {
  vm: {},
  install (Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}

export { installer as VueAxios, service as axios }
